package com.gongpingjialoan.activity.car;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.main.BaseActivity;
import com.gongpingjialoan.bean.Monitor;
import com.gongpingjialoan.bean.RecordBean;
import com.gongpingjialoan.utility.StepMonitor;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by Administrator on 2016/3/18.
 */
public class BuyCarVipActivity extends BaseActivity {

    private View kefuView;

    public NoIntentFragment mNoIntentFragment;

    public IntentFragment mIntentFragment;

    public RecordBean.DataEntity.VipBuyEntity vipBuyEntity;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isSendData = true;
        action = "vipBuy";
        super.onCreate(savedInstanceState);
        setContentView(R.layout.buycar_vip_layout);
        EventBus.getDefault().register(this);
        mNoIntentFragment = new NoIntentFragment();
        mIntentFragment = new IntentFragment();
        mNoIntentFragment.setAnimationStyle(AnimationFragment.CUBE);
        mIntentFragment.setAnimationStyle(AnimationFragment.CUBE);

        boolean show_time_and_note = getIntent().getBooleanExtra("noshow_time_and_note", true);
        setTitle(show_time_and_note ? "VIP买车服务" : "挑选目标车型");
        initView();
    }

    @Override
    public void initTitleBar() {
        View backV;
        String from = getIntent().getStringExtra("from");
        if ("carlist".equals(from)) {
            backV = findViewById(R.id.icon_back);
        } else {
            backV = findViewById(R.id.title_back);
        }
        backV.setVisibility(View.VISIBLE);

        if (backV != null) {


            backV.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    StepMonitor.getInstance().addMonitor(new Monitor("jump", "vipBuy", System.currentTimeMillis(), 1f));
//                    String from = getIntent().getStringExtra("from");
//                    //不为空代表从个人中心的"系统推荐好车过来的",并且没有设置vip筛选条件
//                    if (!TextUtils.isEmpty(from)) {
//                        startActivity(new Intent(BuyCarVipActivity.this, SysRecomendCarActivity.class));
//                    }
                    finish();

                }
            });
        }
    }

    private void initView() {
        kefuView = findViewById(R.id.right_layout);
        kefuView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "4000253500"));
                startActivity(intent);
            }
        });

        //   mIntentFragment.getArguments().putInt("direction", AnimationFragment.RIGHT);
        //   mIntentFragment.setArguments(getBundle(AnimationFragment.RIGHT));
        vipBuyEntity = getIntent().getParcelableExtra("VipBuyEntity");
        if (vipBuyEntity == null) {
            getSupportFragmentManager().beginTransaction().replace(R.id.content, mIntentFragment).commit();
        } else {
            if ("ambiguous".equals(vipBuyEntity.getType())) {
                getSupportFragmentManager().beginTransaction().replace(R.id.content, mNoIntentFragment).commit();
            } else if ("specific".equals(vipBuyEntity.getType())) {
                getSupportFragmentManager().beginTransaction().replace(R.id.content, mIntentFragment).commit();
            }
        }
    }


    private Bundle getBundle(int type) {
        Bundle bundle = new Bundle();
        bundle.putInt("direction", type);
        return bundle;
    }

    public void goNoIntent() {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

//        ft.setCustomAnimations(
//                R.animator.fragment_slide_left_enter,
//                R.animator.fragment_slide_right_exit);
        mNoIntentFragment.setArguments(getBundle(AnimationFragment.RIGHT));
        ft.replace(R.id.content, mNoIntentFragment).commit();
    }

    public void goIntent() {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

//        ft.setCustomAnimations(
//                R.animator.fragment_slide_left_enter,
//                R.animator.fragment_slide_right_exit);
        mIntentFragment.setArguments(getBundle(AnimationFragment.RIGHT));
        ft.replace(R.id.content, mIntentFragment).commit();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refresh(Object o) {
        if ("notification".equals(o.toString())) {
            if (mNoIntentFragment.isAdded()) {
                mNoIntentFragment.initBanner();
            }
            if (mIntentFragment.isAdded()) {
                mIntentFragment.initBanner();
            }
        }
    }
}
